
var gulp = require('gulp'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    runSequence = require('run-sequence'),
    rev = require('gulp-rev'),
    revCollector = require('gulp-rev-collector'),
    cssmin = require('gulp-minify-css'),
    sass = require('gulp-sass');
//定义css、js源文件路径
var cssSrc = "['css/*.css','css/**/*.css']",
    cssPubSrc='public/components/bootstrap/dist/css/*.css',
    jsSrc = 'js/**/*.js',
    jsPubSrc='public/components/jquery/dist/*.js',
    jsAngularSrc='public/components/angular/*.js',
    jsBootSrc='public/components/bootstrap/dist/js/*.js';
    htmlSrc="['page/**/*.html','page/**/**/*.html']";

/*压缩css文件*/
gulp.task('testCssmin', function () {
    gulp.src(['css/common.css',
               'css/style.css',
               'css/viewer.css',
               'css/develop/develop.css'
           ])           //- 需要处理的css文件，放到一个字符串数组里
        .pipe(concat('common.min.css'))    //- 合并后的文件名
        .pipe(cssmin())   //- 压缩处理成一行
        .pipe(rev())        //- 文件名加MD5后缀
        .pipe(gulp.dest('css'));//- 输出文件本地
});
/*压缩js文件*/
gulp.task('testJsmin', function () {
    gulp.src(['js/directive/tip.js',
            'js/directive/agreement.js',
            'js/directive/entry.js',
            'js/directive/datatime.js',
            'js/directive/input.js',
            'js/directive/docSide.js'
        ])           //- 需要处理的css文件，放到一个字符串数组里
        .pipe(concat('mainDirective.js'))
        .pipe(uglify({mangle: false}))
        .pipe(gulp.dest('js/directive'))
});
/*编译scss*/
gulp.task('sassTest', function(){
    return gulp.src(['css/develop/base.scss','css/develop/develop.scss'])
        .pipe(sass())
        .pipe(gulp.dest('css/develop'))
});

//CSS生成文件hash编码并生成 rev-manifest.json文件名对照映射
gulp.task('revCss', function(){
    return gulp.src(['css/*.css','css/**/*.css'])
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/css'));
});
gulp.task('pubCss', function(){
    return gulp.src(cssPubSrc)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/css1'));
});


//js生成文件hash编码并生成 rev-manifest.json文件名对照映射
gulp.task('revJs', function(){
    return gulp.src(jsSrc)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/js'));
});
gulp.task('pubJs', function(){
    return gulp.src(jsPubSrc)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/js1'));
});
gulp.task('AnguJs', function(){
    return gulp.src(jsAngularSrc)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/js2'));
});
gulp.task('bootJs', function(){
    return gulp.src(jsBootSrc)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/js3'));
});

gulp.task('htmlPage', function(){
    return gulp.src(['page/**/*.html','page/**/**/*.html'])
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/html'));
});

//Html替换css、js文件版本
gulp.task('revHtml', function () {
    return gulp.src(['rev/**/*.json', './*.html'])
        .pipe(revCollector())
        .pipe(gulp.dest('.'));
});

//Html替换css、js文件版本
gulp.task('revHtml1', function () {
    return gulp.src(['rev/js/*.json', './js/route/router.js'])
        .pipe(revCollector())
        .pipe(gulp.dest('./js/route'));
});

gulp.task('revHtml2', function () {
    return gulp.src(['rev/html/*.json', './js/route/router.js'])
        .pipe(revCollector())
        .pipe(gulp.dest('./js/route'));
});

//任务区
gulp.task('run', function(){
    gulp.run('watch','versions','testCssmin','testJsmin');
});

//watch
gulp.task('watch', function(){
    //版本信息进行控制
    gulp.watch(['js/**/*.js','js/*.js','css/**.css','page/*.html','page/**/*.html'], ['versions']);
    gulp.watch(['css/develop/develop.css','css/*.css'], ['testCssmin']);
    gulp.watch(['js/directive/*.js'], ['testJsmin']);
});

//开发构建
gulp.task('dev', function (done) {
    condition = false;
    runSequence(
        ['revCss'],
        ['pubCss'],
        ['revJs'],
        ['pubJs'],
        ['AnguJs'],
        ['bootJs'],
        ['htmlPage'],
        ['revHtml'],
        ['revHtml1'],
        ['revHtml2'],
        done);
});


gulp.task('versions', ['dev']);
